package algorithm;

import java.util.Arrays;
import java.util.Comparator;

public class leetcode_0354 {
    public int maxEnvelopes(int[][] envelopes) {
        Arrays.sort(envelopes, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o1[0]==o2[0]){
                    return o2[1]-o1[1];
                }
                return (o1[0]-o2[0]);
            }
        });
        int len=envelopes.length;
        int [] dp=new int[len];
        dp[0]=1;
        int max=1;
        for(int i=1;i<len;++i){
            int tem=0;
            for(int j=0;j<i;++j){
                if(envelopes[j][1]<envelopes[i][1]){
                    tem=(tem>dp[j])?tem:dp[j];
                }
            }
            dp[i]+=(tem+1);
            max=(max>dp[i])?max:dp[i];
        }
        return max;
    }
}
